package com.china08.yxyapi.api.home.activity.healthcompetition;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.huodong.healthcompetition.YxyCompetitionStudent;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.huodong.YxyCompetitionStudentRepository;
import com.china08.yxyapi.security.profile.YxyProfile;

/**
 * 学生列表展示
 * 
 * @author zd.Jin
 *
 *
 */
@RestController
@CrossOrigin
public class StudentCardLstController extends YxyApiBaseController {
	@Value("${yx.rest.api.yx.path}")
	private String yxpath;
	@Autowired
	private YxyCompetitionStudentRepository yxyCompetitionStudentRepository;
	@Autowired
	private RestTemplate restTemplate;

	@RequestMapping(value = "/api_yw/home/activity/healthcompetition/studentcard/list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public List<RespModel> process(HttpServletRequest request, HttpServletResponse response) {
		List<RespModel> respModels = new ArrayList<>();
		// 得到当前登录用户
		YxyProfile profile = getProfile(request, response);
		String userId = profile.getId();
		String roleName = profile.getRoleId();
		// 当前用户是学生时
		if ("ROLE_STUDENT".equals(roleName)) {
			YxyCompetitionStudent yxyCompetitionStudent = yxyCompetitionStudentRepository.findTop1ByStudentId(userId);
			if (yxyCompetitionStudent != null) {
				// 学生名片赋值
				RespModel respModel = new RespModel();
				respModel.checked = 0;
				respModel.studentId = yxyCompetitionStudent.getStudentId();
				respModel.studentName = yxyCompetitionStudent.getStudentNick();
				respModel.schoolName = yxyCompetitionStudent.getSchoolName();
				respModel.className = yxyCompetitionStudent.getClassName();
				respModel.score = yxyCompetitionStudent.getScore();
				respModel.status = yxyCompetitionStudent.getStatus();
				if (yxyCompetitionStudent.getImg() != null && yxyCompetitionStudent.getImg().startsWith("http:")) {
					respModel.img = yxyCompetitionStudent.getImg().replaceFirst("http:", "https:");
				} else {
					respModel.img = yxyCompetitionStudent.getImg();
				}
				respModels.add(respModel);
			}
		}
		// 当前用户是家长时
		else if ("ROLE_PARENTS".equals(roleName)) {
			try {
				HttpHeaders headers = new HttpHeaders();
				MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
				headers.setContentType(type);
				headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
				Map<String, Object> formMap = new HashMap<String, Object>();
				formMap.put("parentId", userId);
				HttpEntity<Map<String, Object>> formEntity = new HttpEntity<Map<String, Object>>(formMap, headers);
				ParRespModel httpResult = restTemplate.postForObject(yxpath + "/comm/par/selectyoungchildid",
						formEntity, ParRespModel.class);
				List<String> students = httpResult.studentInfos;
				// 得到该家长最后以哪个孩子的身份答题的
				YxyCompetitionStudent lastStu = yxyCompetitionStudentRepository
						.findTop1ByStatusNotAndStudentIdInOrderByLastModifiedDateDesc(0, students);
				String lastid = null;
				if (lastStu != null) {
					lastid = lastStu.getStudentId();
				} else {
					lastid = students.get(0);
				}
				for (String student : students) {
					YxyCompetitionStudent yxyCompetitionStudent = yxyCompetitionStudentRepository
							.findTop1ByStudentId(student);
					if (yxyCompetitionStudent != null) {
						// 学生名片赋值
						RespModel respModel = new RespModel();
						// 判断页面展示默认展示哪个孩子的答题
						if (yxyCompetitionStudent.getStudentId().equals(lastid)) {
							respModel.checked = 0;
						} else {
							respModel.checked = 1;
						}
						respModel.studentId = yxyCompetitionStudent.getStudentId();
						respModel.studentName = yxyCompetitionStudent.getStudentNick();
						respModel.schoolName = yxyCompetitionStudent.getSchoolName();
						respModel.className = yxyCompetitionStudent.getClassName();
						respModel.score = yxyCompetitionStudent.getScore();
						respModel.status = yxyCompetitionStudent.getStatus();
						if (yxyCompetitionStudent.getImg() != null
								&& yxyCompetitionStudent.getImg().startsWith("http:")) {
							respModel.img = yxyCompetitionStudent.getImg().replaceFirst("http:", "https:");
						} else {
							respModel.img = yxyCompetitionStudent.getImg();
						}
						respModels.add(respModel);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return respModels;
	}

	// 家长身份远程调用接口返回学生id集合
	protected static class ParRespModel {
		public List<String> studentInfos;
	}

	protected static class RespModel extends YxyApiRespModel {
		// 当前页展示哪个学生的答题数据(0选中1不选中)
		public Integer checked;
		// 学生id
		public String studentId;
		// 学生头像
		public String img;
		// 学生姓名
		public String studentName;
		// 学校名称
		public String schoolName;
		// 班级名称
		public String className;
		// 得分
		public Integer score;
		// 状态(0未开始1进行中2已完成)
		public Integer status;
	}
}
